package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.sampler.NewObjectSample;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	public class WelcomeScreen extends MovieClip
	{
		public static const SHAPE_SCREEN_ALPHA:Number = .85; //.9;
		public static const BACKGROUND_COLOR:uint = 0x88FF88;
		public static const POLYGON_COUNT:uint = 10;
		public var completed:Boolean;
		private var delayInstructionTimer:Timer;
		private var readyToContinue:Boolean = false;
		private var getUserID:Boolean;
		private var beginButton:MyButton, introButton:MyButton;
		//tFields holds the instructions, utFields holds the interactive user textfields
		private var instructs:Array, instructsCharLimits:Array, tfields:Array, utfields:Array;		
		private var titleTField:TextField, introTField:TextField, instrClickTField:TextField;
		private var introText:String;
		private var storyPanel:StoryPanel;
		private var shape_screen:Shape, screen:Shape;
		// for animations of parent shapes
		private var parentPolygons:Array, obstacles:Array;
		
		public function get text1():String{if(utfields.length>0){return utfields[0].text}else{return ""}}
		public function get text2():String{if(utfields.length>1){return utfields[1].text}else{return ""}}
		public function get text3():String{if(utfields.length>2){return utfields[2].text}else{return ""}}
		public function get text4():String{if(utfields.length>3){return utfields[3].text}else{return ""}}
		
		public function WelcomeScreen(introText:String, instructs1:String, instructs1CharLimit:int=0, instructs2:String="", instructs2CharLimit:int=0, instructs3:String="", instructs3CharLimit:int=0, instructs4:String="", instructs4CharLimit:int=0)
		{
			this.introText = introText;
			instructs = new Array(instructs1, instructs2, instructs3, instructs4);
			instructsCharLimits = new Array(instructs1CharLimit, instructs2CharLimit, instructs3CharLimit, instructs4CharLimit);
			this.addEventListener(Event.ADDED, handleAdded);
		} 
		public function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			
			var testpoints:Array = new Array();
			testpoints.push(new Point(.5,.5));testpoints.push(new Point(.4,.3));testpoints.push(new Point(.6,.7));testpoints.push(new Point(.2,.5));testpoints.push(new Point(.8,.5));
			for each (var p:Point in testpoints){p.x = 100*p.x; p.y = 70*p.y;}
			testpoints =  MathUtilities.calcRotatedPoints(315,testpoints, new Point(50,35));
			for each (p in testpoints){p.x = p.x/100; p.y = p.y/70;}
			trace("Rotated points",testpoints);
			
			var tfield:TextField, utfield:TextField;
			this.graphics.beginFill(BACKGROUND_COLOR);
			this.graphics.drawRect(0,0,this.parent.width, this.parent.height);
			this.graphics.endFill();
			// place parent shapes
			parentPolygons = new Array();
			obstacles = new Array();
			for (i=0; i < POLYGON_COUNT; i++)
			{
				// randomly a triangle or quadrilateral
				var shape_type:String;
				if (Math.random() < .5) { shape_type = "triangle"; } else { shape_type = "quadrilateral"; }
				var ps:ParentPolygon = new ParentPolygon(this.width/8, this.width/8, shape_type, "", ShapeProductionTask.TYPES);
				//ps.x = Math.random()*(this.width-this.width/8); ps.y = Math.random()*(this.height-this.height/8);
				this.addChild(ps);
				parentPolygons.push(ps);
				obstacles.push(ps);
			}			
			this.placePolygonsWithoutCollisions(this.width/8, this.height/8, this.width - this.width/8, this.height - this.height/8);
			for each (ps in parentPolygons)
			{
				ps.neighbors = obstacles;
				ps.moveRandomly(this.height, 1, 1);	
			}
			
			shape_screen = new Shape();
			shape_screen.graphics.beginFill(0xFFFFFF, SHAPE_SCREEN_ALPHA);
			shape_screen.graphics.drawRect(0,0,this.parent.width, this.parent.height);
			shape_screen.graphics.endFill();
			this.addChild(shape_screen);
			
			// Place an intro TextField at top of screen centered
			if (introText.length > 0){
				introTField = new TextField();
				introTField.defaultTextFormat = new TextFormat("Arial", this.width/20,0x006600, true);
				introTField.multiline = true;
				introTField.text = introText;
				introTField.autoSize = "left";
				introTField.selectable = false;
				introTField.x = (this.width-introTField.width)/2;
				introTField.y = 40;
				this.addChild(introTField);
			}
			
			tfields = new Array();
			utfields = new Array();
			var textHeight:Number = this.height/2;
			var texty:Number = this.height/4;
			for (var i:int=0; i < instructs.length; i++){
				if (instructs[i].length > 0){
					
					utfield = new TextField();
					utfield.defaultTextFormat = new TextFormat("Arial", this.width/40,0x006600);
					utfield.multiline = false;
					utfield.type = "input";
					utfield.background = true;
					utfield.backgroundColor = 0xEEFFEE;
					utfield.border = true;
					utfield.text = " ";
					utfield.width = int(utfield.defaultTextFormat.size) * 15;
					utfield.height = utfield.textHeight+4;
					utfield.text = "";
					if (instructsCharLimits[i]>0)
					{
						utfield.maxChars = instructsCharLimits[i];
						utfield.width = int(utfield.defaultTextFormat.size) * instructsCharLimits[i] * 1.5;
					}
					utfield.x = (this.width/2 - utfield.width)/2;
					utfield.y = texty + i/instructs.length*textHeight;
					this.addChild(utfield);
					utfields.push(utfield);
					
					tfield = new TextField();
					tfield.defaultTextFormat = new TextFormat("Arial", this.width/40,0x006600);
					tfield.multiline = true;
					tfield.text = instructs[i];
					tfield.autoSize = "left";
					tfield.selectable = false;
					tfield.x = utfield.x + utfield.width + 20;
					tfield.y = utfield.y;
					this.addChild(tfield);
					tfields.push(tfield);
					
				} else {
					break;
				}
			}
			
			instrClickTField = new TextField();
			instrClickTField.defaultTextFormat = new TextFormat("Arial", this.width/20,0x000000);
			instrClickTField.multiline = true;
			instrClickTField.text = " ";
			instrClickTField.autoSize = "left";
			instrClickTField.x = 20;
			instrClickTField.y = texty+textHeight;
			this.addChild(instrClickTField);
		
			beginButton = new MyButton(this.width/5,this.width/20,this.width/30,this.width/40,this.width/40,0xDDDDFF,"Begin", 0x444488,0xCCCCFF,"Begin", 0x444488,0xBBBBFF,"Begin", 0x444488,true,true);
			beginButton.y = instrClickTField.y + instrClickTField.height + 20;
			beginButton.x = (this.width/2-beginButton.width)/2;
			beginButton.addEventListener(Event.COMPLETE, beginPressHandler);
			this.addChild(beginButton);
			
			introButton = new MyButton(this.width/5,this.width/20,this.width/30,this.width/40,this.width/40,0xDDDDFF,"Intro", 0x444488,0xCCCCFF,"Intro", 0x444488,0xBBBBFF,"Intro", 0x444488,true,true);
			introButton.y = beginButton.y;
			introButton.x = this.width/2+(this.width/2-introButton.width)/2;
			introButton.addEventListener(Event.COMPLETE, introPressHandler);
			this.addChild(introButton);
		}
		
		/* Occurs after a delay to instruct user to press red square to continue */
		private function introPressHandler(event:Event):void
		{
			introButton.removeEventListener(Event.COMPLETE, introPressHandler);
			// screen out everything below
			screen = new Shape();
			screen.graphics.beginFill(0xFFFFFF, .7);
			screen.graphics.drawRect(0,0,this.width,this.height);
			screen.graphics.endFill();
			this.addChild(screen);
			storyPanel = new StoryPanel(this.width/2, this.height/2);
			storyPanel.x = this.width/4;
			storyPanel.y = this.height/4;
			storyPanel.addEventListener(Event.COMPLETE, completeIntro);
			this.addChild(storyPanel);
		}
			private function completeIntro(event:Event):void
			{
				storyPanel.removeEventListener(Event.COMPLETE, completeIntro);
				this.removeChild(storyPanel);
				this.removeChild(screen);
				introButton.addEventListener(Event.COMPLETE, introPressHandler);
			}
		
		/* When user presses the button we must check whether the userID is filled out and the labels contain either even or odd */
		private function beginPressHandler(event:Event):void
		{
			// make sure that each textbox includes text
			completed = true;
			for (var i:int=0; i < utfields.length; i++){
				if (utfields[i].text.length <= 0){
					completed = false;
					if (i==0)
					{
						instrClickTField.text = "What's your name?";
					} else if (i==1)
					{
						instrClickTField.text = "What project are you working on?";
					} else
					{
						instrClickTField.text = "You haven't completed all fields.";
					}
					break;
				}
			}
			if (completed){
				for each (var p:ParentPolygon in parentPolygons)
				{
					p.stopMoving();
					this.removeChild(p);
				}
				beginButton.removeEventListener(MouseEvent.CLICK, beginPressHandler);
				//this.removeEventListener (Event.ENTER_FRAME, handleFrameHitTests);				
				this.dispatchEvent(new Event(Event.COMPLETE));
			}					
		}
		/** Compares current display object to all objects on obstacle list */
		private function placePolygonsWithoutCollisions (minX:Number, minY:Number, maxX:Number, maxY:Number):void
		{
			var maxTries:uint = 100;
			var placedObstacles:Array = new Array();
			for each (var o:Polygon in obstacles)
			{
				var tries:uint = 0;
				do 
				{
					var conflict:Boolean = false;
					o.x = minX + Math.random() * (maxX - minX);
					o.y = minY + Math.random() * (maxY - minY);
					for each (var p:Polygon in placedObstacles)
					{
						if (o != p)
						{
							if (o.hitTestPolygon(p) && tries < maxTries)
							{
								//trace(o, p, o.hitTestPolygon(p), tries, maxTries, o.x, o.y);
								conflict = true;
								break;
							}
						}
					}
					tries++;
				} while (conflict)
				placedObstacles.push(o);	
			}
			//obstacles = placedObstacles;
		}
	}
}